package com.feishi.core.util.http;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Sort {
    public static void main(String[] args) throws IOException {
        File file=new File("E:/a/31");
        Map<String, List<String>> map=new HashMap<>();
        file.list((dir, name) -> {
            String[] names = name.split("(\\d*[\\]】])|(第.章)|(\\d+-\\d+)| |-" +
                    "|[\\(（][一二三四五六七八九十]");
            if(names.length==1){
//                System.out.println(name);
                return false;
            }
//            System.out.println(name.substring(0,names[0].length()+1));

            List<String> list = getList(map, names[0].replaceAll("[\\[【]",""));
            list.add(name);
            return false;
        });
//        if(1==1)return;
        for (String s : map.keySet()) {
            System.out.print(s);
            System.out.print("::");
//            map.get(s).sort(String::compareTo);

            String root;
            if (map.get(s).size()==1){
                root="E:/out2";
            }else {
                root="E:/out1/"+s;
            }
            new File(root).mkdirs();

            for (String s1 : map.get(s)) {
                System.out.print(s1);
                Files.copy(new File("E:/a/31/"+s1).toPath(),new FileOutputStream(new File(root+"/"+s1)));
                System.out.print("::");
            };
            System.out.println();
        }
    }

    private static List<String> getList(Map<String, List<String>> map, String name) {
        List<String> list = map.get(name);
        if (list==null)list=new ArrayList<>();
        map.put(name,list);
        return list;
    }

    private static void sort1() {

        File file=new File("E:/book");

        String[] files = file.list((dir, name) -> {
            if(name.matches("^\\s*【[^】]+】.*$")){
                System.out.println(name);
                String ndir=name.replaceAll("^\\s*(【[^】]+】).*$","$1");
                System.out.println(ndir);
                try {
                    new File("E:/out/"+ndir).mkdirs();
                    Files.copy(new File("E:/book/"+name).toPath(),new FileOutputStream(new File("E:/out/"+ndir+"/"+name)));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                try {
                    Files.copy(new File("E:/book/"+name).toPath(),new FileOutputStream(new File("E:/out/"+name)));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            return false;
        });

    }
}
